﻿
// SvgView.cpp : CSvgView 类的实现
//

#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Svg.h"
#endif

#include "SvgDoc.h"
#include "SvgView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "libsvg\libsvg.h"

// CSvgView

IMPLEMENT_DYNCREATE(CSvgView, CScrollView)

BEGIN_MESSAGE_MAP(CSvgView, CScrollView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CSvgView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_MOUSEWHEEL()
	ON_WM_HSCROLL()
	ON_WM_VSCROLL()
END_MESSAGE_MAP()

// CSvgView 构造/析构

CSvgView::CSvgView()
{
	// TODO: 在此处添加构造代码
	ptStart.x=0;
	ptStart.y=0;
}

CSvgView::~CSvgView()
{
}

BOOL CSvgView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CScrollView::PreCreateWindow(cs);
}

// CSvgView 绘制

void CSvgView::OnDraw(CDC* /*pDC*/)
{
	CSvgDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	
	bool ret;
	// TODO: 在此处为本机数据添加绘制代码
	if (!theApp.filename.IsEmpty())
	{
		//way 1
		//ClibSvgService* plibSvgService =new ClibSvgService();
		//if (plibSvgService != NULL) {
		//	plibSvgService->DrawSvgToCDC(theApp.filename, this->GetDC(), ptStart);
		//	delete plibSvgService;
		//	plibSvgService = NULL;
		//}

		//way 2
		ret = fDrawSvgToCDC(theApp.filename, this->GetDC(), ptStart);

		//way 3
		//TCHAR *pSvgPath = theApp.filename.GetBuffer();
		//ret=fDrawSvgToCDC2(pSvgPath, this->GetDC(), ptStart);
	}
}

void CSvgView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: 计算此视图的合计大小
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}


// CSvgView 打印


void CSvgView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

BOOL CSvgView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CSvgView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CSvgView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}

void CSvgView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CSvgView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CSvgView 诊断

#ifdef _DEBUG
void CSvgView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CSvgView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CSvgDoc* CSvgView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSvgDoc)));
	return (CSvgDoc*)m_pDocument;
}
#endif //_DEBUG


// CSvgView 消息处理程序


BOOL CSvgView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if(MK_CONTROL==nFlags)
	{
		if ( zDelta >0)
		{
			theApp.percent+=0.5;
		}
		else
		{
			theApp.percent-=0.5;
		}
		if(theApp.percent<=0)
		{
			theApp.percent=0.005;
		}
		if(theApp.percent>=10)
		{
			theApp.percent=10;
		}

		CRect rect;
		GetClientRect(&rect);
		CSize sizeTotal;
		sizeTotal.cx = rect.right*theApp.percent;
		sizeTotal.cy = rect.bottom*theApp.percent;
		SetScrollSizes(MM_TEXT, sizeTotal);

		Invalidate();
	}
	else
	{
		SCROLLINFO si ;
		GetScrollInfo (SB_VERT,&si,SIF_ALL);
		if(si.nPos<0)
		{
			si.nPos=0;
		}
		ptStart.y = si.nPos;
		Invalidate();
	}

	return CScrollView::OnMouseWheel(nFlags, zDelta, pt);
}


void CSvgView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	SCROLLINFO si;
    GetScrollInfo(SB_HORZ,&si,SIF_ALL);

    ptStart.x = si.nPos;

	Invalidate();

	CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
}


void CSvgView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	SCROLLINFO si ;
    GetScrollInfo (SB_VERT,&si,SIF_ALL);

    ptStart.y = si.nPos;

	Invalidate();

	CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
}
